// pages/test/test.js
var util = require('../../utils/util.js')
const DB = wx.cloud.database()
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //处理导 航 栏的高度
    rpxdata: wx.getStorageSync('rpxdata'),
    navigationBarAndStatusBarHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 'px',
    navigationBarAndStatusBarAndsearchHeight: wx.getStorageSync('statusBarHeight') + wx.getStorageSync('navigationBarHeight') + 60 + 'px',
    //设置导航栏的显示信息
    navbarData: {
      showCapsule: 1,
      title: "人员管理"
    },
    //按钮颜色
    manger: 0,
    btn_1: 1,
    btn_2: 0,
    DB_user: {}
  },

  manger(e) {
    let id = e.currentTarget.dataset.id
    let reg = e.currentTarget.dataset.register
    if (this.data.manger == 0) {
      wx.showToast({
        title: '需要管理员',
        image: '/images/fail.png',
        duration: 2000
      })
    } else {
      if (reg == 0) {
        
        DB.collection("lchuser").doc(id).update({
          data: {
            register: 1
          },
          success: (res) => {
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: (res) => {
            wx.showToast({
              title: '数据库失败',
              image: '/images/fail.png',
              duration: 2000
            })
          }
        })
      } else {
        DB.collection("lchuser").doc(id).update({
          data: {
            register: 0
          },
          success: (res) => {
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: (res) => {
            wx.showToast({
              title: '数据库失败',
              image: '/images/fail.png',
              duration: 2000
            })
          }
        })
      }
    }
  },

  register() {
    let that = this
    DB.collection("lchuser").where({
      register: 1
    }).get({
      success: function (res) {
        that.setData({
          DB_user: res.data,
          btn_1: 1,
          btn_2: 0
        })
      }
    })
  },

  request() {
    let that = this
    DB.collection("lchuser").where({
      register: 0
    }).get({
      success: (res) => {
        that.setData({
          DB_user: res.data,
          btn_1: 0,
          btn_2: 1
        })
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   


  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    let that = this
    this.setData({
      openid: wx.getStorageSync('openid')
    })

    //获取用户是否管理员 
    DB.collection("lchuser").where({
      openid: this.data.openid
    }).get({
      success: (res) => {
        that.setData({
          manger: res.data[0].manger
        })
      }
    })

    DB.collection("lchuser").where({
      register: 1
    }).get({
      success: function (res) {
        that.setdata({
          DB_user: res.data
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})